package com.sky.wsp.jar.utils;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class ZipUtil {
  private static final String ZIP_SEPARATOR = "/";

  private ZipUtil() {
  }

  public static void unZipToDir(File srcFile, File unZipDir) throws IOException {
    ZipFile zip = null;

    try {
      zip = new ZipFile(srcFile, 1);
      Enumeration e = zip.entries();

      while(e.hasMoreElements()) {
        ZipEntry entry = (ZipEntry)e.nextElement();
        File targetFile = new File(unZipDir, entry.getName());
        if (entry.isDirectory()) {
          targetFile.mkdirs();
          if (!targetFile.isDirectory()) {
            throw new IOException("Directory '" + targetFile + "' could not be created");
          }
        } else {
          FileOutputStream out = IOUtil.openOutputStream(targetFile);

          try {
            IOUtil.copyStream(zip.getInputStream(entry), out);
          } finally {
            IOUtil.closeQuietly(out);
          }
        }
      }
    } finally {
      IOUtil.closeQuietly(zip);
    }

  }

  public static void zipToFile(ZipUtil.ProcessFile srcFile, File zipFile) throws IOException {
    zipToFile(new ZipUtil.ProcessFile[]{srcFile}, zipFile);
  }

  public static void zipToFile(ZipUtil.ProcessFile[] processFiles, File zipFile) throws IOException {
    ZipOutputStream zos = null;

    try {
      zos = new ZipOutputStream(IOUtil.openOutputStream(zipFile));
      ZipUtil.ProcessFile[] var3 = processFiles;
      int var4 = processFiles.length;

      for(int var5 = 0; var5 < var4; ++var5) {
        ZipUtil.ProcessFile processFile = var3[var5];
        if (processFile.getFile().exists()) {
          zipFile(processFile.getFile(), zos, processFile.getFile().getName(), processFile.isEncrypt());
        }
      }
    } finally {
      IOUtil.closeQuietly(zos);
    }

  }

  private static void zipFile(File srcFile, ZipOutputStream zos, String toZipName, boolean isEncrypt) throws IOException {
    if (srcFile.isDirectory()) {
      zos.putNextEntry(new ZipEntry(toZipName + "/"));
      File[] var11 = srcFile.listFiles();
      int var5 = var11.length;

      for(int var6 = 0; var6 < var5; ++var6) {
        File file = var11[var6];
        zipFile(file, zos, toZipName + "/" + file.getName(), isEncrypt);
      }

    } else {
      zos.putNextEntry(new ZipEntry(toZipName));
      InputStream in = null;
      in = IOUtil.openInputStream(srcFile);
      IOUtil.openInputStream(srcFile);

      try {
        if (in != null) {
          IOUtil.copyStream(in, zos);
        }
      } finally {
        IOUtil.closeQuietly(in);
      }

    }
  }

  public static class ProcessFile {
    private File file;
    private boolean isEncrypt;

    public ProcessFile(File file, boolean isEncrypt) {
      this.file = file;
      this.isEncrypt = isEncrypt;
    }

    public File getFile() {
      return this.file;
    }

    public void setFile(File file) {
      this.file = file;
    }

    public boolean isEncrypt() {
      return this.isEncrypt;
    }

    public void setEncrypt(boolean encrypt) {
      this.isEncrypt = encrypt;
    }
  }
}
